#include <soc/soc_t20/jzm_jpeg_enc.h>

__place_k0_data__
uint32_t huffenc_t20[HUFNUM][HUFFENC_LEN] = {
  {0x100,  0x101,  0x204,  0x30b,  0x41a,  0x678,  0x7f8,  0x9f6,
   0xf82,  0xf83,  0x30c,  0x41b,  0x679,  0x8f6,  0xaf6,  0xf84,
   0xf85,  0xf86,  0xf87,  0xf88,  0x41c,  0x7f9,  0x9f7,  0xbf4,
   0xf89,  0xf8a,  0xf8b,  0xf8c,  0xf8d,  0xf8e,  0x53a,  0x8f7,
   0xbf5,  0xf8f,  0xf90,  0xf91,  0xf92,  0xf93,  0xf94,  0xf95,
   0x53b,  0x9f8,  0xf96,  0xf97,  0xf98,  0xf99,  0xf9a,  0xf9b,
   0xf9c,  0xf9d,  0x67a,  0xaf7,  0xf9e,  0xf9f,  0xfa0,  0xfa1,
   0xfa2,  0xfa3,  0xfa4,  0xfa5,  0x67b,  0xbf6,  0xfa6,  0xfa7,
   0xfa8,  0xfa9,  0xfaa,  0xfab,  0xfac,  0xfad,  0x7fa,  0xbf7,
   0xfae,  0xfaf,  0xfb0,  0xfb1,  0xfb2,  0xfb3,  0xfb4,  0xfb5,
   0x8f8,  0xec0,  0xfb6,  0xfb7,  0xfb8,  0xfb9,  0xfba,  0xfbb,
   0xfbc,  0xfbd,  0x8f9,  0xfbe,  0xfbf,  0xfc0,  0xfc1,  0xfc2,
   0xfc3,  0xfc4,  0xfc5,  0xfc6,  0x8fa,  0xfc7,  0xfc8,  0xfc9,
   0xfca,  0xfcb,  0xfcc,  0xfcd,  0xfce,  0xfcf,  0x9f9,  0xfd0,
   0xfd1,  0xfd2,  0xfd3,  0xfd4,  0xfd5,  0xfd6,  0xfd7,  0xfd8,
   0x9fa,  0xfd9,  0xfda,  0xfdb,  0xfdc,  0xfdd,  0xfde,  0xfdf,
   0xfe0,  0xfe1,  0xaf8,  0xfe2,  0xfe3,  0xfe4,  0xfe5,  0xfe6,
   0xfe7,  0xfe8,  0xfe9,  0xfea,  0xfeb,  0xfec,  0xfed,  0xfee,
   0xfef,  0xff0,  0xff1,  0xff2,  0xff3,  0xff4,  0xff5,  0xff6,
   0xff7,  0xff8,  0xff9,  0xffa,  0xffb,  0xffc,  0xffd,  0xffe,
   0x30a,  0xaf9,  0xfff,  0xfff,  0xfff,  0xfff,  0xfff,  0xfff,
   0xfd0,  0xfd1,  0xfd2,  0xfd3,  0xfd4,  0xfd5,  0xfd6,  0xfd7,
   0x101,  0x204,  0x30a,  0x418,  0x419,  0x538,  0x678,  0x8f4,
   0x9f6,  0xbf4,  0x30b,  0x539,  0x7f6,  0x8f5,  0xaf6,  0xbf5,
   0xf88,  0xf89,  0xf8a,  0xf8b,  0x41a,  0x7f7,  0x9f7,  0xbf6,
   0xec2,  0xf8c,  0xf8d,  0xf8e,  0xf8f,  0xf90,  0x41b,  0x7f8,
   0x9f8,  0xbf7,  0xf91,  0xf92,  0xf93,  0xf94,  0xf95,  0xf96,
   0x53a,  0x8f6,  0xf97,  0xf98,  0xf99,  0xf9a,  0xf9b,  0xf9c,
   0xf9d,  0xf9e,  0x53b,  0x9f9,  0xf9f,  0xfa0,  0xfa1,  0xfa2,
   0xfa3,  0xfa4,  0xfa5,  0xfa6,  0x679,  0xaf7,  0xfa7,  0xfa8,
   0xfa9,  0xfaa,  0xfab,  0xfac,  0xfad,  0xfae,  0x67a,  0xaf8,
   0xfaf,  0xfb0,  0xfb1,  0xfb2,  0xfb3,  0xfb4,  0xfb5,  0xfb6,
   0x7f9,  0xfb7,  0xfb8,  0xfb9,  0xfba,  0xfbb,  0xfbc,  0xfbd,
   0xfbe,  0xfbf,  0x8f7,  0xfc0,  0xfc1,  0xfc2,  0xfc3,  0xfc4,
   0xfc5,  0xfc6,  0xfc7,  0xfc8,  0x8f8,  0xfc9,  0xfca,  0xfcb,
   0xfcc,  0xfcd,  0xfce,  0xfcf,  0xfd0,  0xfd1,  0x8f9,  0xfd2,
   0xfd3,  0xfd4,  0xfd5,  0xfd6,  0xfd7,  0xfd8,  0xfd9,  0xfda,
   0x8fa,  0xfdb,  0xfdc,  0xfdd,  0xfde,  0xfdf,  0xfe0,  0xfe1,
   0xfe2,  0xfe3,  0xaf9,  0xfe4,  0xfe5,  0xfe6,  0xfe7,  0xfe8,
   0xfe9,  0xfea,  0xfeb,  0xfec,  0xde0,  0xfed,  0xfee,  0xfef,
   0xff0,  0xff1,  0xff2,  0xff3,  0xff4,  0xff5,  0xec3,  0xff6,
   0xff7,  0xff8,  0xff9,  0xffa,  0xffb,  0xffc,  0xffd,  0xffe,
   0x100,  0x9fa,  0xfff,  0xfff,  0xfff,  0xfff,  0xfff,  0xfff,
   0xfd0,  0xfd1,  0xfd2,  0xfd3,  0xfd4,  0xfd5,  0xfd6,  0xfd7,
   0x100,  0x202,  0x203,  0x204,  0x205,  0x206,  0x30e,  0x41e,
   0x53e,  0x67e,  0x7fe,  0x8fe,  0xfff,  0xfff,  0xfff,  0xfff,
   0x100,  0x101,  0x102,  0x206,  0x30e,  0x41e,  0x53e,  0x67e,
   0x7fe,  0x8fe,  0x9fe,  0xafe,  0xfff,  0xfff,  0xfff,  0xfff}
};

__place_k0_data__
uint32_t qmem_t20[QTNUM][QMEM_LEN] = {
  //normal quantization table
  {0x0100, 0x0155, 0x0155, 0x0a49, 0x0155, 0x0b33, 0x0100, 0x0a49,
   0x0a49, 0x0a49, 0x09c7, 0x09c7, 0x0100, 0x00cd, 0x0955, 0x08cd,
   0x093b, 0x0955, 0x12e9, 0x12e9, 0x0955, 0x251f, 0x11c7, 0x11af,
   0x0911, 0x08cd, 0x1a35, 0x113b, 0x2421, 0x1111, 0x1a35, 0x113b,
   0x1a49, 0x1a49, 0x0880, 0x19c7, 0x10b2, 0x10d2, 0x0880, 0x10f1,
   0x10ba, 0x10ea, 0x1a49, 0x1a49, 0x10cd, 0x1095, 0x231f, 0x10ba,
   0x1955, 0x229d, 0x193b, 0x193b, 0x193b, 0x2421, 0x10d2, 0x223f,
   0x2219, 0x2249, 0x10a4, 0x1911, 0x10b2, 0x2d05, 0x193b, 0x10a4,
   0x09c7, 0x09c7, 0x09c7, 0x0955, 0x12e9, 0x0955, 0x1155, 0x093b,
   0x093b, 0x1155, 0x10a4, 0x23e1, 0x1a49, 0x23e1, 0x10a4, 0x10a4,
   0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4,
   0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4,
   0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4,
   0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4,
   0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4,
   0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4, 0x10a4,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
  //finer quantization table
  {0x0155, 0x0200, 0x0200, 0x0b33, 0x0200, 0x0200, 0x0155, 0x0b33,
   0x0b33, 0x0b33, 0x0155, 0x0155, 0x0155, 0x0155, 0x0100, 0x093b,
   0x09c7, 0x0100, 0x0a49, 0x0a49, 0x0100, 0x0911, 0x12e9, 0x0955,
   0x09c7, 0x093b, 0x11c7, 0x0080, 0x11af, 0x11af, 0x11c7, 0x0080,
   0x11c7, 0x08f1, 0x08cd, 0x08ba, 0x1a49, 0x1155, 0x08cd, 0x08c3,
   0x1a5f, 0x08c3, 0x08f1, 0x11c7, 0x251f, 0x23e1, 0x251f, 0x1a5f,
   0x1a35, 0x1111, 0x0880, 0x0880, 0x0880, 0x11af, 0x1155, 0x10ea,
   0x19bb, 0x10f1, 0x2421, 0x19bb, 0x1a49, 0x2421, 0x0880, 0x1111,
   0x0155, 0x0155, 0x0155, 0x0100, 0x0a49, 0x0100, 0x0911, 0x09c7,
   0x09c7, 0x0911, 0x1111, 0x08c3, 0x11c7, 0x08c3, 0x1111, 0x1111,
   0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111,
   0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111,
   0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111,
   0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111,
   0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111,
   0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111, 0x1111,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000},
  //finest quantization table
  {0x02ab, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x02ab, 0x0400,
   0x0400, 0x0400, 0x02ab, 0x02ab, 0x02ab, 0x02ab, 0x02ab, 0x0b33,
   0x0200, 0x02ab, 0x02ab, 0x02ab, 0x02ab, 0x0155, 0x0200, 0x0b33,
   0x0200, 0x0b33, 0x0a49, 0x0155, 0x0a49, 0x0a49, 0x0a49, 0x0155,
   0x0a49, 0x0155, 0x0a49, 0x0100, 0x00cd, 0x09c7, 0x0a49, 0x0100,
   0x00cd, 0x0100, 0x0155, 0x0a49, 0x09c7, 0x0955, 0x09c7, 0x00cd,
   0x00cd, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x0a49, 0x09c7, 0x0955,
   0x093b, 0x0955, 0x12e9, 0x093b, 0x00cd, 0x12e9, 0x12e9, 0x12e9,
   0x02ab, 0x02ab, 0x02ab, 0x02ab, 0x02ab, 0x02ab, 0x0155, 0x0200,
   0x0200, 0x0155, 0x12e9, 0x0100, 0x0a49, 0x0100, 0x12e9, 0x12e9,
   0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9,
   0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9,
   0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9,
   0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9,
   0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9,
   0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9, 0x12e9,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}
};

#define EFE_HALN8_FLAG(en)   (((en) & 0x1)<<7)
void T20_JPEGE_SliceInit(_JPEGE_SliceInfo *s)
{
  unsigned int i;
  unsigned int alig_flag = 1;
  volatile unsigned int *chn = (volatile unsigned int *)s->des_va;

  GEN_VDMA_ACFG(chn, TCSM_FLUSH, 0, 0x0);

  /* Open clock configuration */
  GEN_VDMA_ACFG(chn, REG_JPGC_GLBI, 0, OPEN_CLOCK);

  /**************************************************
   Huffman Encode Table configuration
  *************************************************/
  for(i=0; i<HUFFENC_LEN; i++)
    GEN_VDMA_ACFG(chn, REG_JPGC_HUFE+i*4, 0, huffenc_t20[s->huffenc_sel][i]);

  /**************************************************
   Quantization Table configuration
  *************************************************/
  for(i=0; i<QMEM_LEN; i++)
    GEN_VDMA_ACFG(chn, REG_JPGC_QMEM+i*4, 0,s->qmem_table[i]);

  /**************************************************
   REGs configuration
  *************************************************/
  GEN_VDMA_ACFG(chn, REG_JPGC_STAT, 0, STAT_CLEAN);
  GEN_VDMA_ACFG(chn, REG_JPGC_BSA, 0, (intptr_t)s->bsa);

  if(s->raw_format) { /* NV12 or NV21 */
    GEN_VDMA_ACFG(chn, REG_JPGC_P0A, 0, VRAM_RAWY_BA);
  } else {/* TILE */
    GEN_VDMA_ACFG(chn, REG_JPGC_P0A, 0, (intptr_t)s->p0a);
    GEN_VDMA_ACFG(chn, REG_JPGC_P1A, 0, (intptr_t)s->p1a);
  }

  GEN_VDMA_ACFG(chn, REG_JPGC_NMCU, 0, s->nmcu);
  GEN_VDMA_ACFG(chn, REG_JPGC_NRSM, 0, s->nrsm);

  GEN_VDMA_ACFG(chn, REG_JPGC_P0C, 0, YUV420P0C);
  GEN_VDMA_ACFG(chn, REG_JPGC_P1C, 0, YUV420P1C);
  GEN_VDMA_ACFG(chn, REG_JPGC_P2C, 0, YUV420P2C);

  GEN_VDMA_ACFG(chn, REG_JPGC_MAX_BS, 0, ((0<<31) | (1<<22)) );


  if(s->raw_format) {
    GEN_VDMA_ACFG(chn, REG_JPGC_GLBI, 0, (YUV420PVH                 |
					  JPGC_NCOL                 |
					  JPGC_SPEC     /* MODE */  |
					  JPGC_EFE                  |
					  JPGC_EN));

    GEN_VDMA_ACFG(chn, REG_JPGC_TRIG, 0, JPGC_BS_TRIG | JPGC_PP_TRIG | JPGC_CORE_OPEN);

  }else{
    GEN_VDMA_ACFG(chn, REG_JPGC_GLBI, 0, (YUV420PVH                 |
					  JPGC_NCOL                 |
					  JPGC_SPEC     /* MODE */  |
					  JPGC_EN));

    GEN_VDMA_ACFG(chn, REG_JPGC_TRIG, VDMA_ACFG_TERM, JPGC_BS_TRIG | JPGC_PP_TRIG | JPGC_CORE_OPEN);
  }


  /**************************************************
   EFE configuration
  *************************************************/
  uint8_t align8_flag = (s->frame_height & 0xF) != 0;
  uint8_t work_mode = 0;

  if(s->raw_format) {
	  GEN_VDMA_ACFG(chn, REG_EFE_GEOM, 0, (EFE_JPGC_LST_MBY(s->mb_height-1) |
					 EFE_JPGC_LST_MBX(s->mb_width-1) ) );

    GEN_VDMA_ACFG(chn, REG_EFE_RAWY_SBA, 0, (intptr_t)s->raw[0]);
    GEN_VDMA_ACFG(chn, REG_EFE_RAWU_SBA, 0, (intptr_t)s->raw[1]);
    GEN_VDMA_ACFG(chn, REG_EFE_RAWV_SBA, 0, (intptr_t)s->raw[2]);

    GEN_VDMA_ACFG(chn, REG_EFE_RAW_STRD, 0, (EFE_RAW_STRDY(s->stride[0]) |
					     EFE_RAW_STRDC(s->stride[1]) ) );

    GEN_VDMA_ACFG(chn, REG_EFE_RAW_DBA, 0, VRAM_RAWY_BA);

	if (((unsigned int)(s->raw[0])) & (0x1 << 31)) {
		if (s->frame_width % 128)
			work_mode = 1;
		else
			work_mode = 0;
	} else {
		work_mode = 0;
	}
	GEN_VDMA_ACFG(chn, REG_EFE_CTRL, VDMA_ACFG_TERM, (EFE_PLANE_NV12 |
				EFE_ID_JPEG    |
				EFE_EN         |
				(s->raw_format)|
				(work_mode << 29) |
				EFE_HALN8_FLAG(align8_flag)|
				EFE_RUN));
  }
}
